/*************************************************************************
> File Name: bubble_sort.c
> Author: icebear
> Mail: 3322366195@qq.com
> Created Time: 2023-07-20 21:54:11
> Description: 冒泡排序
************************************************************************/
#include "general.h"
#include <stdio.h>
int bubbleSort(int arr[], int length);
int main(int argc, char const *argv[])
{
    int arr[] = {9, 4, 2, 7, 5, 1, 8, 3, 6, 0};
    int size = sizeof(arr) / sizeof(arr[0]);
    printf("Original array: \n");
    printArr(arr, size);
    bubbleSort(arr, size);
    printf("Sorted array: \n");
    printArr(arr, size);
    return 0;
}
int bubbleSort(int arr[], int length)
{
    //*******************大数沉底小数冒泡*******************
    int i = 0, j = 0;
    int isOrder = 0;
    for (i = 0; i < length - 1; i++)
    {
        isOrder = 1; // 假设有序
        for (j = 0; j < length - i - 1; j++)
        {
            if (arr[j] > arr[j + 1]) // 比较相邻两个数
            {
                swap(&arr[j], &arr[j + 1]);
                if (isOrder)
                {
                    isOrder = 0; // 设置无序
                }
            }
        }
        if (isOrder) // 有序
        {
            break;
        }
    }
}
